home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / smallc.arc / SORT.C < prev    next >
Text File  |  1985-07-17  |  10KB  |  230 lines

  1. #include          <stdio.h>
  2. #include          <ctype.h>
  3.  
  4. #define  MAXWORD  80                           /* maximum word length */
  5. #define  NEWLINE  putc('\n',fp)     /* put carraige return in file fp */
  6.  
  7. struct text {               /* global declaration of tree node 'text' */
  8.              char word[MAXWORD];
  9.              struct text *lptr;
  10.              struct text *rptr;
  11. };
  12.  
  13. struct text *root;         /* global declaration of root of text tree */
  14.  
  15. main(argc, argv)
  16.   /*-----------------------------------------------------------------*/
  17.   /* driver for sort procedure.  Srt will sort lines of a specified  */
  18.   /* input file and place the result in either the stdout or a       */
  19.   /* specified output file.  Program is written for Lattice C but    */
  20.   /* functions provided in its library are documented so that srt    */
  21.   /* might be modified to suit your compiler.  If you have any       */
  22.   /* trouble or questions give me a call (if you can find me)        */
  23.   /*-----------------------------------------------------------------*/
  24.   /* Joe R Wyatt    2311 49th Lubbock, Tx 79412                      */
  25.   /* Home #: 806/793-5689    Office #: 806/763-8011 ext: 239         */
  26.   /*-----------------------------------------------------------------*/
  27.   /* Variables :        fp --     pointer to input file              */
  28.   /*                   fp2 --     ptr to output file                 */
  29.   /*                     c --     integer returned from getw.        */
  30.   /*                     i --     processed line count               */
  31.   /*                     j --     empty line count                   */
  32.   /*                  word --     temporary input line storage       */
  33.   /* Procedures:      getw --     included                           */
  34.   /*                 putwt --     included                           */
  35.   /*                 ptree --     included                           */
  36.   /* Functions :    printf --     string compare                     */
  37.   /*                 fopen --     open file and return file pointer  */
  38.   /*                  exit --     premature exit from program        */
  39.   /*                fclose --     close file                         */
  40.   /*-----------------------------------------------------------------*/
  41. int argc;
  42. char *argv[];
  43. {
  44.      FILE *fp, *fp2, *fopen();
  45.      int c, i, j;
  46.      char word[MAXWORD];
  47.  
  48.      root = NULL;                                /* root of text tree */
  49.      if (argc == 1)  {                  /* no file specified for sort */
  50.           printf("usage: srt infile.ext <outfile.exe>\n");
  51.           exit(0);
  52.      }
  53.      else{                                         /* open input file */
  54.           if ((fp = fopen(*(argv+1), "r")) == NULL) {
  55.                printf("cannot read %s\n",*(argv+1));
  56.                exit(1);
  57.           }
  58.           if (argc == 3) {                        /* open output file */
  59.                if ((fp2 = fopen(*(argv+2), "w")) == NULL) {
  60.                     printf("cannot open %s \n",*argv[3]);
  61.                     exit(1);
  62.                }
  63.           }
  64.           else
  65.                fp2 = stdout;                   /* no output specified */
  66.      }
  67.      i = 0;
  68.      j = 0;
  69.      while((c = getw(fp,word)) != EOF) {     /* get next line of file */
  70.           if (strlen(word) != 0) {
  71.                putwt(word);           /* if not null then put in tree */
  72.                i++;            /* increment number of lines processed */
  73.           }
  74.           else
  75.                j++;                 /* else inc number of empty lines */
  76.      }
  77.      fclose(fp);
  78.      ptree(fp2, root);                           /* print sorted tree */
  79.      printf("\nprocedure concluded.\n%d lines processed\n",i);
  80.      printf("%d empty lines ignored.\n",j);
  81.      fclose(fp2);
  82. }
  83.  
  84. putwt(w)
  85.   /*-----------------------------------------------------------------*/
  86.   /* procedure places 'w' in tree named 'text'.                      */
  87.   /* Parameters:         w --     null ended string                  */
  88.   /* Variables :      temp --     temporary pointer                  */
  89.   /*                   ptr --     ptr to node to place 'w' in.       */
  90.   /*                 value --     result of strcmp used for placement*/
  91.   /*                              in text tree.                      */
  92.   /* Procedures:    search --     included                           */
  93.   /* Functions :    strcmp --     string compare                     */
  94.   /*                getmem --     allocate memory for tree           */
  95.   /*                sizeof --     return size of structure           */
  96.   /*                printf --     print string to stdout             */
  97.   /*                strcpy --     string copy                        */
  98.   /*                  exit --     leave prgram prematurly            */
  99.   /*-----------------------------------------------------------------*/
  100. char *w;
  101. {
  102.      struct text *search(), *temp, *ptr;
  103.      int value = 1;
  104.  
  105.      temp = search(w);  /* locate position to place new node in tree */
  106.  
  107.      if (temp != NULL) /* if NULL then tree is empty. else compare keys */
  108.           value = strcmp(w, temp -> word);
  109.      if (value == 0)                    /* duplicate entry.  ignore. */
  110.           return;
  111.  
  112.      ptr = (struct text *)getmem(sizeof(struct text));
  113.      if (ptr == NULL) {                   /* memory allocation error */
  114.           printf("out of memory\n");
  115.           exit(1);
  116.      }
  117.        strcpy(ptr -> word, w);                     /* store new node */
  118.        ptr -> lptr = NULL;
  119.        ptr -> rptr = NULL;
  120.  
  121.      if (temp != NULL) {                   /* place new node in tree */
  122.           if (value > 0)
  123.                temp -> rptr = ptr;
  124.           else
  125.                temp -> lptr = ptr;
  126.           }
  127.      else
  128.           root = ptr;
  129.      return;
  130. }
  131.  
  132. struct text *search(w)
  133.   /*-----------------------------------------------------------------*/
  134.   /* procedure locates where 'w' is to be placed in text tree and    */
  135.   /* returns pointer to prior node.                                  */
  136.   /* procedure places 'w' in tree named 'text'.                      */
  137.   /* Parameters:         w --     null ended string                  */
  138.   /* Variables :      ptr1 --     temporary pointer                  */
  139.   /*                  ptr2 --     lags ptr1 by one node              */
  140.   /*                 value --     result of strcmp used for placement*/
  141.   /*                              in text tree.                      */
  142.   /* Functions :    strcmp --     string compare                     */
  143.   /*-----------------------------------------------------------------*/
  144. char *w;
  145. {
  146.      struct text *ptr1;
  147.      struct text *ptr2;
  148.      int value;
  149.  
  150.      ptr1 = root;                             /* initialize pointers */
  151.      ptr2 = root;
  152.      while(ptr1 != NULL) {
  153.           ptr2 = ptr1;
  154.           value = strcmp(w, ptr1 -> word);
  155.           if (value > 0)
  156.                ptr1 = ptr1 -> rptr;
  157.           else if (value < 0)
  158.                ptr1 = ptr1 -> lptr;
  159.           else
  160.                ptr1 = NULL;
  161.      }
  162.      return(ptr2);
  163. }
  164.  
  165. ptree(fp, ptr)
  166.   /*-----------------------------------------------------------------*/
  167.   /* recursive procedure to print contents of text tree.             */
  168.   /* Parameters:        fp --     pointer to output file.            */
  169.   /*                   ptr --     pointer to root of current subtree */
  170.   /* Procedures:      putw --     included                           */
  171.   /*-----------------------------------------------------------------*/
  172. FILE *fp;
  173. struct text *ptr;
  174. {
  175.      if (ptr != NULL) {
  176.           ptree(fp, ptr -> lptr);   /* find leftmost node of subtree */
  177.           putw(fp, ptr -> word);                       /* print data */
  178.           ptree(fp, ptr -> rptr);             /* print right subtree */
  179.      }
  180. }
  181.  
  182. #define EOW -2    /* end of word */
  183. getw(fp,w)
  184.   /*-----------------------------------------------------------------*/
  185.   /* gets next line in input file.                                   */
  186.   /* Parameters:        fp --     pointer to input file              */
  187.   /*                     w --     pointer to string for input        */
  188.   /* Variables :         c --     input character                    */
  189.   /*                     f --     end of word flag                   */
  190.   /*                 count --     number of characters               */
  191.   /* Functions :      getc --     get byte from input file           */
  192.   /*-----------------------------------------------------------------*/
  193. FILE *fp;
  194. char *w;
  195. {
  196.      int c,f,count;
  197.  
  198.      f = 0;                                 /* initialize local vars */
  199.      count = 0;
  200.      while (f != EOW && count < MAXWORD) {
  201.           c = getc(fp);
  202.           if (c != EOF && c != '\n') {       /* input to end of line */
  203.                count++;                    /* increment byte counter */
  204.                *w++ = c;
  205.           }
  206.           else
  207.                f = EOW; /* end of line reached or max chars exceded. */
  208.      }
  209.      *w = '\0';                  /* place null byte on end of string */
  210.      return(c);                         /* return last byte of input */
  211. }
  212.  
  213. putw(fp,w)
  214.   /*-----------------------------------------------------------------*/
  215.   /* prints string to output file.                                   */
  216.   /* Parameters:        fp --     pointer to input file              */
  217.   /*                     w --     pointer to string for input        */
  218.   /* Variables :         c --     output character                   */
  219.   /* Functions :      putc --     put byte to output file            */
  220.   /*-----------------------------------------------------------------*/
  221. FILE *fp;
  222. char *w;
  223. {
  224.      int c;
  225.  
  226.      while (*w != '\0')
  227.           putc(*w++, fp);
  228.      NEWLINE;
  229. }
  230.